package com.macro.mall.portal.service.impl;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mapper.UmsAboutUsMapper;
import com.macro.mall.mapper.UmsFeedbackMapper;
import com.macro.mall.model.UmsAboutUs;
import com.macro.mall.model.UmsAboutUsExample;
import com.macro.mall.model.UmsFeedback;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.service.AboutUsService;
import com.macro.mall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AboutUsServiceImpl implements AboutUsService {

    @Autowired
    private UmsAboutUsMapper aboutUsMapper;
    @Autowired
    private UmsFeedbackMapper feedbackMapper;
    @Autowired
    private UmsMemberService memberService;

    @Override
    public CommonResult<UmsAboutUs> getAboutUs() {
        List<UmsAboutUs> umsAboutUses = aboutUsMapper.selectByExampleWithBLOBs(new UmsAboutUsExample());
        if (umsAboutUses != null && umsAboutUses.size() > 0) {
            return CommonResult.success(umsAboutUses.get(0));
        } else {
            return CommonResult.failed("当前没有关于我们");
        }
    }

    @Override
    public CommonResult createFeedback(UmsFeedback feedback) {
        UmsMember currentMember = memberService.getCurrentMember();
        feedback.setMemberId(currentMember.getId());
        feedback.setCreateTime(new Date());
        int count = feedbackMapper.insertSelective(feedback);
        if(count > 0){
            return CommonResult.success(count);
        }else {
            return CommonResult.failed();
        }
    }
}
